import 'package:flutter/material.dart';
import 'package:flutter_syt/theme/font_styles.dart';
import 'package:flutter_syt/theme/theme_color.dart';

enum SubscriptionManageItemMode {
  subscribe,
  unSubscribe,
}

class SubscriptionManageItem extends StatefulWidget {
  final String name;
  final SubscriptionManageItemMode mode;
  final bool subscribed;
  final ValueChanged<String>? onSubscribe;
  final ValueChanged<String>? onUnSubscribe;

  const SubscriptionManageItem(
    this.name, {
    this.subscribed = false,
    this.mode = SubscriptionManageItemMode.subscribe,
    this.onSubscribe,
    this.onUnSubscribe,
  });

  @override
  _SubscriptionManageItemState createState() => _SubscriptionManageItemState();
}

class _SubscriptionManageItemState extends State<SubscriptionManageItem> {
  Image iconRender() {
    String iconPath = 'assets/icons/icon_circle_add.png';
    if (widget.mode == SubscriptionManageItemMode.unSubscribe) {
      iconPath = 'assets/icons/icon_circle_minus.png';
    } else if (widget.subscribed) {
      iconPath = 'assets/icons/icon_circle_add_disabled.png';
    }
    return Image.asset(
      iconPath,
      width: 18,
    );
  }

  clickOntap() {
    if (widget.mode == SubscriptionManageItemMode.unSubscribe) {
      widget.onUnSubscribe!(widget.name);
    } else if (!widget.subscribed) {
      widget.onSubscribe!(widget.name);
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        clickOntap();
        // if (widget.mode == SubscriptionManageItemMode.unSubscribe) {
        //   widget.onUnSubscribe!(widget.name);
        // } else if (!widget.subscribed) {
        //   widget.onSubscribe!(widget.name);
        // }
      },
      child: Container(
        height: 60.0,
        decoration: BoxDecoration(
          color: ThemeColor.bkColorFFFFFF,
          borderRadius: BorderRadius.all(
            Radius.circular(8.0),
          ),
        ),
        child: Stack(
          clipBehavior: Clip.none,
          children: [
            Container(
              padding:
                  EdgeInsets.only(top: 10, right: 14, bottom: 10, left: 10),
              alignment: Alignment.centerLeft,
              child: Text(
                widget.name,
                style: FontStyles.headerTextStyle_14.copyWith(
                  fontWeight: FontWeight.w400,
                  color: widget.mode == SubscriptionManageItemMode.subscribe &&
                          widget.subscribed
                      ? ThemeColor.colorBlack20
                      : ThemeColor.colorBlack100,
                ),
                maxLines: 2,
                softWrap: false,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            Positioned(
                top: -8,
                right: -8,
                child: GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    clickOntap();
                  },
                  child: iconRender(),
                )),
          ],
        ),
      ),
    );
  }
}
